import Vuex from '@wepy/x'
import login from './login'
import home from './home'
import management from './management'
import register from './register'
import managementDetail from './managementDetail'
import divideRecords from './divideRecords'
import settleCard from './settleCard'
import myIncome from './myIncome'
import merchantTrade from './merchantTrade'
import settlementPrice from './settlementPrice'
import auth from './auth'
import changeFee from './changeFee'
import setPassword from './setPassword'
import verifyIdentity from './verifyIdentity'
import withdrawals from './withdrawals'
import device from './device'
import transfer from './transfer'
import balance from './balance'

export default new Vuex.Store({
  state: {
    counter: 0
  },
  mutations: {
    increment (state) {
      state.counter++
    },
    decrement (state) {
      state.counter--
    },
    save(state, { field, payload }) {
      state[field] = { ...state[field], ...payload }
    }
  },
  actions: {
    increment ({ commit }) {
      commit('increment')
    },
    decrement ({ commit }) {
      commit('decrement')
    },
    incrementAsync ({ commit }) {
      setTimeout(() => {
        commit('increment')
      }, 1000)
    },

    login() {
      return new Promise((resolve, reject) => {
        wx.login({
          success: (res) => {
            wx.setStorageSync('auth_code', res.code)
            resolve(res.code)
          },
          fail: () => {
            reject('')
          }
        })
        // wx.checkSession({
        //   success: () => {
        //     const auth_code = wx.getStorageSync('auth_code')
        //     resolve(auth_code)
        //   },
        //   fail: () => {
        //   }
        // })
      })
    }
  },

  modules: {
    login,
    home,
    management,
    register,
    managementDetail,
    divideRecords,
    settleCard,
    myIncome,
    merchantTrade,
    settlementPrice,
    auth,
    changeFee,
    setPassword,
    verifyIdentity,
    withdrawals,
    device,
    transfer,
    balance
  }
})
